package com.example.myshools.Mine.organization.historyActivityManage;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myshools.Mine.organization.historyActivityManage.entity.Activity;
import com.example.myshools.R;
import com.example.myshools.entity.Constant;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HistoryActivity extends AppCompatActivity {
    private ImageView returnToMine;
    private ListView listView;
    private OkHttpClient okHttpClient;
    private List<Activity> AcList=new ArrayList<>();
    private int num=0;//当前页数
    private int oId;
    private int onLoad=0;//是否是上拉加载
    private int position=0;
    private HistoryItemAdapter itemAdapter;
    private RefreshLayout refreshLayout;//刷新加载
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_activity);

        oId=getSharedPreferences("organization",MODE_PRIVATE).getInt("oId",1);
        //获取数据
        Log.e("oId",oId+"");
        listView=findViewById(R.id.history_list);
        itemAdapter=new HistoryItemAdapter(HistoryActivity.this,AcList,R.layout.history_activity_list_item);
        listView.setAdapter(itemAdapter);
        initData();

        returnToMine=findViewById(R.id.activity_manage_back);


        refreshLayout= findViewById(R.id.history_refreshLayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(HistoryActivity.this));

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                initData();
                refreshlayout.finishRefresh(2000);///*,false*/传入false表示刷新失败
            }
        });
     /*   refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                onLoad=1;
                num=num+1;
                initData();
                refreshlayout.finishLoadMore(2000);
            }
        });*/
        returnToMine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    private void initData() {
        OkHttpClient okHttpClient = new OkHttpClient();
        FormBody body=new FormBody.Builder().add("oId", String.valueOf(oId)).build();
        final Request request = new
                Request.Builder().url(Constant.url + "activities/findHistoryActivityByOrgId").post(body).build();
        final Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message=new Message();
                message.what=1;
                handler.sendMessage(message);

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                Gson gson=new Gson();
                List<Activity> list=gson.fromJson(string,new TypeToken<List<Activity>>(){}.getType());

                AcList.clear();
                for (Activity activity:list){

                    AcList.add(activity);
                }

                Log.e("ac",AcList.toString());

                Message message = new Message();
                message.what = 2;
                message.obj = string;
                handler.sendMessage(message);
            }
        });
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                Toast toast4 = Toast.makeText(HistoryActivity.this, "网络连接失败", Toast.LENGTH_SHORT);
                toast4.setGravity(Gravity.CENTER, 0, 0);
                toast4.show();
            }
            if(msg.what==2){
                Log.e("ac",AcList.toString());
                itemAdapter.notifyDataSetChanged();
            }
        }
    };
}
